#include "main.h"

N5110 lcd(PC_7, PA_9, PB_10, PB_5, PB_3, PA_10);
Ticker ticker;
uint16_t screen_slow=0, stick_slow=0, led_slow=0,gyro_slow=0,up_comp_slow=0;
uint16_t flag_ms=0,led_flag_ms=0,role_death_time=0,rgb_time=0;
// main() runs in its own thread in the OS
BufferedSerial pc(USBTX, USBRX, 9600);


int main()
{
    init();
    
    while (true) 
    {
        screen_show();
        key_read();
        buzzer_sound();
        led_main_show();
        data_send();
        data_Read(imudata);
        
    }
    
}

void init() {
    gyro_init();
    lcd.init(LPH7366_1);
    lcd.setContrast(0.5);
    lcd.setBrightness(1.0);
    StickInit();
    init_energy_points();
    init_npcs_randomly();
    init_heart_points();
    ticker.attach(&timer_isr, 1ms);
    encoder_init();
    music_flag = 1;// boot music
}

void timer_isr()
{
    if (++key_slow == 5) key_slow = 0;
    if (++screen_slow == 20) screen_slow = 0;
    if (++flag_ms == 1000) flag_ms = 0;
    if (++led_slow == 100) led_slow=0;
    if (++led_flag_ms == 1000) flag_ms = 0;
    if (++gyro_slow == 20) gyro_slow = 0;
    if (++up_comp_slow == 500) up_comp_slow = 0;
    if (imu_lock>0) imu_lock--;
    if (role_heat_flag==1)
    {
        if (++role_death_time==5000) 
        {
            role_death_time = 0;   
            role_heat_flag = 0;
        }
    }
}
